import store from '@/store'
import router, { asyncRoutes, constantRoutes } from '@/router'
// import { convertRouter, filterAsyncRoutes } from '@/utils/handleRoutes'
// 判断用户权限组是否和routes中meta规定的路由相同
function hasPermission(permissions, route) {
  if (route.meta && route.meta.rightList) {
    // console.log(permissions, 'hasPermission')
    return permissions.some(role => route.meta.rightList.includes(role))
  } else {
    return true
  }
}
// 过滤路由
export function filterAsyncRoutes(routes, permissions) {
  const finallyRoutes = []
  routes.forEach(route => {
    const item = { ...route }
    if (hasPermission(permissions, item)) {
      if (item.children) {
        item.children = filterAsyncRoutes(item.children, permissions)
      }
      finallyRoutes.push(item)
    }
  })
  return finallyRoutes
}
export function addDynamicRoutes(permissions) {
  // debugger
  const menu = store.state.user.menuTree
  const loopRoute = childrenRoute => {
    childrenRoute.forEach(item => {
      item.hidden = true
      item.meta.keepAlive = false
      if (item.children && item.children.length > 0) {
        delete item.meta.keepAlive
        loopRoute(item.children)
      }
    })
  }

  asyncRoutes.forEach(item => {
    item.hidden = true
    if (item.meta.flag !== 1) {
      if (item.children && item.children.length > 0) {
        loopRoute(item.children)
      }
    } else {
      item.meta.keepAlive = false
    }
  })

  const accessedRoutes = matchAsyncRoutes(menu, asyncRoutes)
  console.log(accessedRoutes, store.state.user.menuTree, '路由')
  //  accessRoutes =  filterAsyncRoutes
  // 动态添加路由
  accessedRoutes.forEach(item => router.addRoute(item))
}

export function matchAsyncRoutes(menu, asyncRoutes, index) {
  const accessedRoutes = []

  // 路由层级 控制菜单左边距
  const level = index === undefined ? 0 : index + 1

  if (asyncRoutes) {
    for (let i = 0; i < asyncRoutes.length; i++) {
      const route = asyncRoutes[i]
      for (let j = 0; j < menu.length; j++) {
        const item = menu[j]
        if (route.path === item.path) {
          route.hidden = false
          route.meta.level = level
          accessedRoutes.push(route)
          // 递归判断子节点
          if (item.children && item.children.length > 0) {
            // 自动重定向到下级可用路由
            // for (let k = 0; k < route.children.length; k++) {
            //   const child = route.children[k]
            //   if (!child.hidden) {
            //     // route.redirect = route.path + child.path
            //     route.redirect = child.path
            //     break
            //   }
            // }
            route.redirect = item.children[0].path
            matchAsyncRoutes(item.children, route.children, route.meta.level)
          }
        }
      }
    }
  }
  const permissions = store.getters['user/permissions'] ? store.getters['user/permissions'] : []
  const finallyAsyncRoutes = filterAsyncRoutes([...accessedRoutes], permissions)

  return finallyAsyncRoutes
}

export function getNavList() {
  const menu = store.state.user.menuTree
  let navList = []

  navList = constantRoutes.concat(matchAsyncRoutes(menu, asyncRoutes))
  return navList
}
